// pages/new_boxcheck/new_boxcheck.js
const DB = wx.cloud.database().collection("new_menber")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    new_name: "",
    new_qq: "",
    new_tmpImgList: [],
    new_imgList: [],
    new_level: null,
    new_grade: null,
    new_gem: null,
    new_stone: null,
    // show:0
  },

  // 获取新人游戏内昵称
  get_name(event) {
    this.setData({
      new_name: event.detail.value
    })
    // console.log(this.data.new_name)
  },

  //获取新人QQ
  get_qq(event) {
    this.setData({
      new_qq: event.detail.value
    })
    // console.log(event.detail.value)
  },


  //获取新人游戏等级
  get_level(event) {
    this.setData({
      new_level: event.detail.value
    })
    // console.log(event.detail.value)
  },
  //获取新人上次会战分数
  get_grade(event) {
    this.setData({
      new_grade: event.detail.value
    })
  },
  //获取新人宝石个数
  get_gem(event) {
    this.setData({
      new_gem: event.detail.value
    })
  },
  //获取新人女神石个数
  get_stone(event) {
    this.setData({
      new_stone: event.detail.value
    })
  },
  //获取box截图临时链接
  add_box() {
    let that = this;
    wx.chooseImage({
      count: 5,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: chooseResult => {
        // 将图片临时链接传给new_tmpImgList
        // new_tmpImgList = chooseResult.tempFilePaths
        //检查后缀
        let num = 0;
        let size = 0;
        // console.log(chooseResult.tempFilePaths.length)
        if (chooseResult.tempFilePaths.length > 4) {
          return wx.showToast({
            title: '最多4张图!',
            icon: 'none',
            image: '',
            duration: 2000,
            mask: true,
          })
        } else {
          for (var i = 0; i < chooseResult.tempFilePaths.length; i++) {
            //获取后缀名
            let index = chooseResult.tempFilePaths[i].lastIndexOf('.');
            let suffix = chooseResult.tempFilePaths[i].substr(index + 1);
            // console.log(suffix)
            //获得文件大小
            wx.getFileInfo({
              filePath: chooseResult.tempFilePaths[i],
              success(res) {
                size = res.size;
                // console.log(size)
                //检查文件后缀名
                if (suffix != 'png' && suffix != 'jpg' && suffix != 'jpeg' && suffix != 'bmp') {
                  return wx.showToast({
                    title: '只能上传.png、.jpg、.jpep、.bmp 格式!',
                    icon: 'none',
                    image: '',
                    duration: 2000,
                    mask: true,
                  })
                }
                //检查文件大小
                else if (size > 5000000) {
                  return wx.showToast({
                    title: '单张大小不能超过4MB!',
                    icon: 'none',
                    image: '',
                    duration: 2000,
                    mask: true,
                  })
                } else {
                  num = num + 1;
                }
              }
            })
          }
          wx.showLoading({
            title: '添加中',
            mask: true
          })
          setTimeout(() => {
            // console.log("num=", num)
            if (num == chooseResult.tempFilePaths.length) {
              that.setData({
                new_tmpImgList: chooseResult.tempFilePaths
              })
              wx.hideLoading()
              wx.showToast({
                title: '添加成功!',
                icon: 'success',
                duration: 2000,
                mask: 'true'
              })
            }
          }, 2000);
        }
      },
    })
  },

  //上传所有数据至数据库
  updata() {
    let that = this;
    // let dict = {};
    //把图片上传至云存储
    // console.log("tmpImgList:", that.data.new_tmpImgList)
    // console.log("imgList:",this.data.new_imgList)
    //检测tmp图片list是否为空
    if (that.data.new_tmpImgList.length == 0) {
      wx.showModal({
        title: '提示',
        content: '未添加box截图!',
        success(res) {
          if (res.confirm) {
            // console.log('用户点击确定')
          } else if (res.cancel) {
            // console.log('用户点击取消')
          }
        }
      })
    } else {
      let flag = 0;
      wx.showLoading({
        title: '上传中',
        mask: true
      })
      for (var i = 0; i < that.data.new_tmpImgList.length; i++) {
        let dict = {};
        wx.cloud.uploadFile({
          // 指定上传到的云路径
          cloudPath: that.data.new_qq + '_' + i + '.png',
          // 指定要上传的文件的小程序临时文件路径
          filePath: that.data.new_tmpImgList[i],
          // 成功回调
          success: res => {
            // console.log('上传成功', res.fileID)
            dict['imgUrl'] = res.fileID
            that.data.new_imgList.push(dict)
            flag = flag + 1;
            // console.log('成功存储id至data', that.data.new_imgList)
          },
        })
      };
      setTimeout(() => {
        if (flag == that.data.new_imgList.length) {
          DB.add({
            data: {
              _id:that.data.new_qq,
              new_name: that.data.new_name,
              new_qq: that.data.new_qq,
              new_level: that.data.new_level,
              new_grade: that.data.new_grade,
              new_gem: that.data.new_gem,
              new_stone: that.data.new_stone,
              new_imgList: that.data.new_imgList,
              new_success:'n'
            }
          })
          wx.hideLoading()
          wx.reLaunch({
            url: "../success/success"
          })
        }
      }, 12000);
    }
  },

  //下拉刷新
  onPullDownRefresh: function () {
    // console.log("触发刷新");
    var that = this;
    setTimeout(() => {
      that.setData({
        new_name: "",
        new_qq: "",
        new_tmpImgList: [],
        new_imgList: [],
        new_level: null,
        new_grade: null,
        new_gem: null,
        new_stone: null,
      })
      this.onLoad(); //重新加载onLoad()
      wx.showToast({
        title: '刷新成功!',
        icon: 'none',
        image: '',
        duration: 1000,
        mask: true,
      })
    }, 1500);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.stopPullDownRefresh()
    // console.log("暂停刷新成功")
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

})